#include "heroplane.h"
#include "config.h"
#include "bullet.h"

HeroPlane::HeroPlane(bool isPlayer1)
{
    m_Free=false;
    // 设置玩家类型
    m_isPlayer1 = isPlayer1;

    // 根据玩家类型加载不同的飞机图片
   m_Plane.load(isPlayer1 ? HERO_PATH : HERO2_PATH);

    // 初始化飞机坐标 - 根据玩家类型设置不同初始位置
    if(isPlayer1) {
        // 玩家1在左侧
        m_X = (GAME_WIDTH * 0.25) - (m_Plane.width() * 0.5);
    } else {
        // 玩家2在右侧
        m_X = (GAME_WIDTH * 0.75) - (m_Plane.width() * 0.5);
    }

    m_Y = GAME_HEIGHT - m_Plane.height();

    //初始化矩形边框
    m_Rect.setWidth(m_Plane.width());
    m_Rect.setHeight(m_Plane.height());
    m_Rect.moveTo(m_X, m_Y);

    //初始化发射间隔记录
    m_recorder = 0;
    //初始化额外子弹状态和时间
        m_hasExtraBullets = false;
        m_extraBulletsTime = 0;

    // 初始化子弹 - 根据玩家类型设置不同子弹外观
    for(int i = 0; i < BULLET_NUM; i++) {
        if(isPlayer1) {
            m_bullets[i].m_Bullet.load(BULLET_PATH); // 玩家1的子弹
        } else {
            m_bullets[i].m_Bullet.load(BULLET2_PATH); // 玩家2的子弹
        }
        m_bullets[i].m_Free = true;
        m_bullets[i].m_X = 0;
        m_bullets[i].m_Y = 0;
        m_bullets[i].m_Speed = BULLET_SPEED;
        m_bullets[i].m_Rect.setWidth(m_bullets[i].m_Bullet.width());
        m_bullets[i].m_Rect.setHeight(m_bullets[i].m_Bullet.height());
        m_bullets[i].m_Rect.moveTo(m_bullets[i].m_X, m_bullets[i].m_Y);
    }

}

void HeroPlane::shoot()
{
    //更新额外子弹状态
         updateExtraBullets();
    //累加时间间隔记录变量
    m_recorder++;

    //如果记录的数字未达到发射子弹的时间间隔，直接return，不发射子弹
    if(m_recorder < BULLET_INTERVAL) {
        return;
    }

    //达到发射时间
    m_recorder = 0;

    //发射子弹
    for(int i = 0; i < BULLET_NUM; i++) {
        //如果是空闲的子弹，进行发射
        if(m_bullets[i].m_Free) {
            //改为非空闲
            m_bullets[i].m_Free = false;
            //设置子弹坐标
            m_bullets[i].m_X = m_X + m_Rect.width() * 0.5 - 10;
            m_bullets[i].m_Y = m_Y - 25;
            break;
        }
    }

    if(m_hasExtraBullets){
            for (int i = 0; i < BULLET_NUM; ++i) {
                        if (m_bullets[i].m_Free) {
                            m_bullets[i].m_X = m_X + 5;  // 左侧位置
                            m_bullets[i].m_Y = m_Y - 20;
                            m_bullets[i].m_Free = false;
                            break;
                        }
                    }

                    for (int i = 0; i < BULLET_NUM; ++i) {
                        if (m_bullets[i].m_Free) {
                            m_bullets[i].m_X = m_X + m_Rect.width() - 20;  // 右侧位置
                            m_bullets[i].m_Y = m_Y - 20;
                            m_bullets[i].m_Free = false;
                            break;
                        }
                    }
        }
}

void HeroPlane::setPosition(int x, int y)
{
    m_X = x;
    m_Y = y;
    m_Rect.moveTo(m_X, m_Y);
}

void HeroPlane::activateExtraBullets(int duration)
{
    m_hasExtraBullets = true;
    m_extraBulletsTime = duration;
}

void HeroPlane::updateExtraBullets()
{
    if (!m_hasExtraBullets)
    {
        return;
    }
    m_extraBulletsTime -= BULLET_INTERVAL;  // 减少剩余时间

    if (m_extraBulletsTime <= 0)
    {
        m_hasExtraBullets = false;  // 时间到，取消额外子弹
    }
}
